﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InfraWEB.DAL;
using InfraWEB.DAL.ItemInspecaoTableAdapters;

namespace InfraWEB.BLL
{
    [System.ComponentModel.DataObject]
	public class Sala
	{
        private SalaTableAdapter _salaAdapter = null;
        protected SalaTableAdapter Adapter
        {
            get
            {
                if (_salaAdapter == null)
                {
                    _salaAdapter = new SalaTableAdapter();
                }
                return _salaAdapter;
            }
        }

        /// <summary>
        /// Obtém todos as salas.
        /// </summary>
        /// <returns>ItemInspecao.Sala.Datatable</returns>
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public InfraWEB.DAL.ItemInspecao.SalaDataTable GetAllSalas()
        {
            return Adapter.GetAllSalas();
        }

        /// <summary>
        /// Obtém sala pelo Id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>ItemInspecao.Sala.Datatable</returns>
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public InfraWEB.DAL.ItemInspecao.SalaDataTable GetSalasBySalId(int id)
        {
            return Adapter.GetSalaBySalId(id);
        }
		
		[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public InfraWEB.DAL.ItemInspecao.SalaDataTable GetSalasByPreId(int id)
        {
            return Adapter.GetSalasByPreId(id);
        }
		
        /// <summary>
        /// Persiste sala no banco de dados.
        /// </summary>
        /// <param name="nome"></param>
        /// <param name="tipo"></param>
        /// <param name="preId"></param>
        /// <returns>true (caso a sala seja persistido com sucesso)</returns>
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool RecordSala(string nome, int tipo, int preId)
        {
            // Cria uma instância nova de SalaRow
            InfraWEB.DAL.ItemInspecao.SalaDataTable salaTable = new InfraWEB.DAL.ItemInspecao.SalaDataTable();
            InfraWEB.DAL.ItemInspecao.SalaRow salaRow = salaTable.NewSalaRow();
            if (nome == string.Empty)
            {
                // Erro: campos obrigatórios nulos
                return false;
            }

            salaRow.SalNome = nome;
            salaRow.SalTipo = tipo;
            salaRow.PreId   = preId;

            // Persiste a nova sala
            salaTable.AddSalaRow(salaRow);
            int rowsAffected = Adapter.Update(salaTable);

            // Retorna true se for persistida exatamente uma única linha, caso contrário, false
            return (rowsAffected == 1);
        }

	}
}
